package controller

import (
	"errors"
	"gitee.com/fierce_wolf/go-fox-edge-common/commRedisExchange"
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/Map"
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/Method"
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/Sort"
	"gitee.com/fierce_wolf/go-fox-edge-common/httpserver"
	"go-fox-edge-kernel-manager/utils/PageUtils"
	"strings"
)

type exchangeManageController struct {
}

func (e *exchangeManageController) init() {
	httpserver.Server.POST("/exchange/entities", e.selectEntityList)
	httpserver.Server.POST("/exchange/page", e.selectPageList)
	httpserver.Server.POST("/exchange/entity", e.insertEntity)
	httpserver.Server.PUT("/exchange/entity", e.updateEntity)
	httpserver.Server.POST("/exchange/entity/delete", e.deleteEntity)
}

func (e *exchangeManageController) selectEntityList(context httpserver.Context, body map[string]interface{}) (interface{}, error) {
	return e.selectList(context, body, false)
}

func (e *exchangeManageController) selectPageList(context httpserver.Context, body map[string]interface{}) (interface{}, error) {
	return e.selectList(context, body, true)
}

func (e *exchangeManageController) selectList(context httpserver.Context, body map[string]interface{}, isPage bool) (interface{}, error) {
	pageNum := Map.GetInt(body, "pageNum", 1)
	pageSize := Map.GetInt(body, "pageSize", 10)

	// 查找数据
	entityList, err := commRedisExchange.FindConfigValues(func(data map[string]interface{}) bool {
		serviceName := strings.TrimSpace(Map.GetString(body, "serviceName", ""))
		if serviceName != "" && !strings.Contains(Map.GetString(data, "serviceName", ""), serviceName) {
			return false
		}

		configName := strings.TrimSpace(Map.GetString(body, "configName", ""))
		if configName != "" && !strings.Contains(Map.GetString(data, "configName", ""), configName) {
			return false
		}

		return true
	})
	if err != nil {
		return nil, err
	}

	// 对数据进行排序
	Sort.Sort(entityList, func(v1, v2 interface{}) bool {
		data1 := v1.(map[string]interface{})
		data2 := v2.(map[string]interface{})

		name1 := Map.GetString(data1, "serviceName", "") + ":" + Map.GetString(data1, "configName", "")
		name2 := Map.GetString(data2, "serviceName", "") + ":" + Map.GetString(data2, "configName", "")
		return name1 < name2
	})

	// 获得分页数据
	if isPage {
		return PageUtils.GetMapPageList(entityList, pageNum, pageSize), nil
	} else {
		return PageUtils.GetMapPageList(entityList, 1, 1000), nil
	}
}

func (e *exchangeManageController) insertEntity(context httpserver.Context, body map[string]interface{}) (interface{}, error) {
	delete(body, "id")
	return e.insertOrUpdate(context, body)
}

func (e *exchangeManageController) updateEntity(context httpserver.Context, body map[string]interface{}) (interface{}, error) {
	return e.insertOrUpdate(context, body)
}

func (e *exchangeManageController) insertOrUpdate(context httpserver.Context, body map[string]interface{}) (interface{}, error) {
	// 提取业务参数
	configName := Map.GetString(body, "configName", "")
	serviceType := Map.GetString(body, "serviceType", "")
	serviceName := Map.GetString(body, "serviceName", "")
	configValue := Map.GetMap(body, "configValue", nil)

	// 简单校验参数
	if Method.HasEmpty(configName, serviceName, serviceType) || Method.HasNull(configValue) {
		return nil, errors.New("参数不能为空:configName, serviceName, serviceType, configValue")
	}

	err := commRedisExchange.SaveConfigValue(serviceType, serviceName, configName, configValue)
	return nil, err
}

func (e *exchangeManageController) deleteEntity(context httpserver.Context, body map[string]interface{}) (interface{}, error) {
	// 提取业务参数
	configName := Map.GetString(body, "configName", "")
	serviceType := Map.GetString(body, "serviceType", "")
	serviceName := Map.GetString(body, "serviceName", "")

	// 简单校验参数
	if Method.HasEmpty(configName, serviceName, serviceType) {
		return nil, errors.New("参数不能为空:configName, serviceName, serviceType")
	}

	err := commRedisExchange.RemoveConfigValue(serviceType, serviceName, configName)
	return nil, err
}
